# encoding: utf-8
from django.db import models
from django.db import connection
import datetime
import calendar
# Create your models here.


class User(models.Model):
    user = models.CharField(u'姓名全拼', max_length=32)
    name = models.CharField(u'姓名中文', max_length=32, null=True, blank=True)
    balance = models.FloatField(u'余额')

    class Meta:
        ordering = ('name', )

    def __unicode__(self):
        return self.name if self.name else self.user

    @property
    def today_foods(self):
        today = datetime.date.today()
        return Record.objects.filter(user=self, target=today)

    @property
    def histor_orders(self):
        return Record.objects.filter(user=self)


class Food(models.Model):
    name = models.CharField(u'菜品', max_length=32)
    begin = models.DateField(u'开始日期', null=True, blank=True)
    end = models.DateField(u'结束日期', null=True, blank=True)
    price = models.FloatField(u'价格')

    @property
    def hint(self):
        d1 = datetime.datetime.now()
        d2 = datetime.datetime(d1.year, d1.month, d1.day, 0, 0, 0, 0)
        return Record.objects.filter(food=self).filter(oprdate__gt=d2).count()

    @property
    def is_first(self):
        '''
        @summary: 是否是上旬菜式
        '''
        today = datetime.date.today()
        first_day = datetime.date(today.year, today.month, 1)
        second_day = datetime.date(today.year, today.month, 10)
        return "cur" if self.begin == first_day and self.end == second_day else ""

    @property
    def is_second(self):
        '''
        @summary: 是否是中旬菜式
        '''
        today = datetime.date.today()
        second_day_plus = datetime.date(today.year, today.month, 11)
        third_day = datetime.date(today.year, today.month, 20)
        return "cur" if self.begin == second_day_plus and self.end == third_day else ""

    @property
    def is_third(self):
        '''
        @summary: 是否是下旬菜式
        '''
        today = datetime.date.today()
        max_day = calendar.monthrange(today.year, today.month)[1]
        third_day_plus = datetime.date(today.year, today.month, 21)
        end_day = datetime.date(today.year, today.month, max_day)
        return "cur" if self.begin == third_day_plus and self.end == end_day else ""

    @property
    def is_whole(self):
        '''
        @summary: 是否是全月菜式
        '''
        today = datetime.date.today()
        first_day = datetime.date(today.year, today.month, 1)
        max_day = calendar.monthrange(today.year, today.month)[1]
        end_day = datetime.date(today.year, today.month, max_day)
        return "cur" if self.begin == first_day and self.end == end_day else ""

    def __unicode__(self):
        return self.name


class RecordManager(models.Manager):
    def sum_money(self):
        cursor = connection.cursor()
        res = cursor.execute("""
            select sum(price) from lunch_record where date(oprdate) = date('now')
        """)
        return res.fetchone()[0]

    def collect(self):
        cursor = connection.cursor()
        res = cursor.execute("""
            select food, count(1) count from lunch_record
                where date(oprdate) = date('now') group by food order by count desc

        """)
        return res.fetchall()


class Record(models.Model):
    oprdate = models.DateTimeField(u'操作日期时间')
    target = models.DateField(u'点餐目标日期')
    user = models.ForeignKey(User, verbose_name=u'用户')
    food = models.CharField(u'菜品', max_length=32,null=True,blank=True)
    price = models.FloatField(u'价格')
    objects = RecordManager()

    def __unicode__(self):
        return '%s on %s' % (self.user.user, self.oprdate)


class PaidItem(models.Model):
    target = models.DateField(u'日期')
    user = models.ForeignKey(User)
    epaid = models.FloatField(u'员工付')
    cpaid = models.FloatField(u'公司付')


class Recommend(models.Model):
    target = models.DateField(u'推荐日期')
    score = models.FloatField(u'推荐指数')
    user = models.ForeignKey(User)
    food = models.ForeignKey(Food)
